package org.karelun.rabbit.rcp;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;
import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.karelun.rabbit.graph.Diagram;
import org.karelun.rabbit.graph.GraphFactory;
import org.karelun.rabbit.graph.GraphPackage;
import org.karelun.rabbit.rcp.editor.RabbitEditorInput;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }

    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    public void preWindowOpen() {
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setInitialSize(new Point(400, 300));
        configurer.setShowCoolBar(true);
        configurer.setShowMenuBar(true);
        configurer.setShowStatusLine(true);
        configurer.setTitle("Rabbit"); //$NON-NLS-1$
    }
    
    public void postWindowOpenss() {
    	String server="localhost";
		String respository="rabbit";
		
		IConnector connector = Net4jUtil.getConnector(IPluginContainer.INSTANCE, "tcp", server);
		CDONet4jSessionConfiguration config = CDONet4jUtil.createNet4jSessionConfiguration();
		config.setConnector(connector);
		config.setRepositoryName(respository);
		
		CDONet4jSession session = config.openNet4jSession();
		session.getPackageRegistry().putEPackage(GraphPackage.eINSTANCE);
		CDOTransaction transaction = session.openTransaction();
		transaction.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);
		
//		String resourcePath="rsdj";
//	    if(!transaction.hasResource(resourcePath))
//	    {
	    	CDOResource resource = transaction.createResource("rsdj");
	    	Diagram diagram = GraphFactory.eINSTANCE.createDiagram();
		    diagram.setText("hhh");
		    resource.getContents().add(diagram);
		    try {
				transaction.commit();
			} catch (CommitException e) {
				e.printStackTrace();
			}
//	    }
    	
	    RabbitEditorInput editorInput = new RabbitEditorInput(transaction, resource.getPath());
    	IWorkbenchPage page = getWindowConfigurer().getWindow().getActivePage();
    	try {
			page.openEditor(editorInput, "org.karelun.rabbit.editor");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
    }
}
